package com.example.bbgds.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;


/**
 * Created by bbgds on 2016/4/7.
 */
public class VideoActivity extends Activity implements View.OnClickListener{

    View main ;

    private VideoView mVideoView;
    private MediaController mMediaController;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        main = getLayoutInflater().from(this).inflate(R.layout.activity_video, null);
        main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        main.setOnClickListener(this);
        setContentView(main);
        mImageView = (ImageView)findViewById(R.id.imgae);
        mImageView.setImageBitmap(getShadowBitmap(this, R.drawable.ic_launcher, null));
//        setContentView(R.layout.activity_video);
//        mVideoView = (VideoView)findViewById(R.id.video1);
//        mMediaController = new MediaController(this);
//        initPlayer();
    }

    public static Bitmap getReflectionedBitmap(Context context, int resourceId, Bitmap originalImage, int reflectionGap) {
        if (originalImage == null)
            originalImage = BitmapFactory.decodeResource(context.getResources(), resourceId);

        int width = originalImage.getWidth();
        int height = originalImage.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        // Create a Bitmap with the flip matix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
                height / 2, width, height / 2, matrix, false);

        // Create a new bitmap with same width but taller to fit reflection

        Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
                (height + height / 2), Bitmap.Config.ARGB_8888);
        // Create a new Canvas with the bitmap that's big enough for
        // the image plus gap plus reflection

        Canvas canvas = new Canvas(bitmapWithReflection);
        // Draw in the original image
        canvas.drawBitmap(originalImage, 0, 0, null);
        // Draw in the gap
        Paint deafaultPaint = new Paint();
        deafaultPaint.setColor(0xffffffff);
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);

        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0,
                originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
                + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
        // Set the paint to use this shader (linear gradient)
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
                + reflectionGap, paint);
        originalImage.recycle();
        originalImage = null;
        return bitmapWithReflection;
    }

    public static Bitmap getShadowBitmap(Context context, int resourceId, Bitmap originalImage) {
        if (originalImage == null){
            originalImage = BitmapFactory.decodeResource(context.getResources(), resourceId);
        }
        int width = originalImage.getWidth();
        int height = originalImage.getHeight();
        Bitmap shadowBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(shadowBitmap);
        // Draw in the original image
        canvas.drawBitmap(originalImage, 0, 0, null);
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, shadowBitmap.getHeight(), 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawRect(0, 0, width, height, paint);
        originalImage.recycle();
        originalImage = null;
        return shadowBitmap;
    }

    private void initPlayer(){
//        File file = new File(Environment.getExternalStorageDirectory() + "/160401114750102.mp4");
//        Log.e("zhu","sss=" + Environment.getExternalStorageDirectory() + "," + file.exists());
//        if(file.exists()){
//            mVideoView.setVideoPath(file.getAbsolutePath());
//            mVideoView.setMediaController(mMediaController);
//            mVideoView.setOnErrorListener(videoOnErrorListener);
//            mVideoView.setOnCompletionListener(myVideoViewCompletionListener);
//            mMediaController.setMediaPlayer(mVideoView);
//            mVideoView.requestFocus();
//            mVideoView.start();
//        }
    }


    private MediaPlayer.OnErrorListener videoOnErrorListener = new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.i("zhu", "Not played");
            return true;
        }
    };

    MediaPlayer.OnCompletionListener myVideoViewCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mVideoView.stopPlayback();
            Log.i("zhu", "onCompletion");
            mVideoView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    initPlayer();
                }
            }, 3000);
        }
    };

    @Override
    public void onClick(View v) {
        int i = main.getSystemUiVisibility();
        if (i == View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) {
            main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        } else if (i == View.SYSTEM_UI_FLAG_VISIBLE){
            main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        } else if (i == View.SYSTEM_UI_FLAG_LOW_PROFILE) {
            main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    }
}
